let app = getApp();

Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    userInfo: {},
    isAvatar: false,
    vip_day: 0,
    jindu: [
      { id: 1, name: 'ئوقۇۋاتقان', iconName: 'du.svg' },
      { id: 2, name: 'ئوقۇپ بولغان', iconName: 'wancheng.svg' },
      { id: 3, name: 'ئاڭلاۋاتقان', iconName: 'ting.svg' },
      { id: 4, name: 'ئاڭلاپ بولغان', iconName: 'wancheng-kok.svg' },
    ],
    filterBg: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 点击进度
    onClickInUse(e) {
      let listId = e.currentTarget.dataset.item;
      wx.navigateTo({
        url: `/pages/inUse/inUse?listId=${listId}`,
      })
    },

    // 点击 List
    onClickList(e) {
      let listId = parseInt(e.currentTarget.dataset.id, 10);
      switch (listId) {
        case 0:
          // wx.navigateTo({
          //   url: '/pages/personalInfo/personalInfo',
          // })
          break;
        case 1:
          wx.navigateTo({
            url: '/pages/member/member',
          })
          break;
        case 2:
          wx.navigateTo({
            url: '/pages/contact/contact',
          })
          break;
        case 3:
          wx.navigateTo({
            url: '/pages/books/books',
          })
          break;
        case 4:
          // wx.navigateTo({
          //   url: '/pages/aboutu/aboutu', author
          // })
          break;
        case 5:
          this.setData({ filterBg: true })
          
          break;
        
        case 6:
          wx.navigateTo({
            url: '/pages/authorCon/authorCon',
          })
          
          break;
        case 7:
          wx.navigateTo({
            url: '/pages/proxy/proxy',
          })
          
          break;
        default:
          console.log("点击错误 ~");
          break;
      }
      
    },

    // 检查用户登录
    isLogin() {
      let userInfo = wx.getStorageSync('userInfo')
      this.setData({ isAvatar: wx.getStorageSync('isAvatar') });
      if (!userInfo) {
        app.login( res => {
          this.setData({ userInfo: res.user })
        })
      } else {
        this.setData({ userInfo: userInfo.user })
      }
    },

    // 获取头像
    onChooseAvatar(e) {
      const { avatarUrl } = e.detail;
      this.setData({ 'userInfo.avatarUrl': avatarUrl })

      let userInfo = wx.getStorageSync('userInfo')
      
      let userId = userInfo.user.id;
      let that = this;
      wx.request({
        method: 'POST',
        url: `${app.globalData.API_URL}/updateAvatar`,
        data: { userId, avatarUrl },
        success(res) {
          if(res.statusCode == 200) {
            userInfo.user.avatarUrl = avatarUrl;
            that.setData({ isAvatar: true });
            wx.setStorageSync('userInfo', userInfo);
            wx.setStorageSync('isAvatar', true);
          }
        }
      })
    },

    // 获取书籍统计
    getTotalCountBook() {
      let that = this;
      wx.request({
        method: 'GET',
        url: `${app.globalData.API_URL}/getTotalBook`,
        success(res) {
          if(res.statusCode == 200) {
            that.setData({ totalCount: res.data.count_book });
          }
        }
      })
    },

    // 确定缓存
    confrimClear() {
      this.setData({ filterBg: false })
      // 清除所有本地缓存
      wx.clearStorageSync();

      setTimeout(() => {
        // 强制重启小程序
        wx.reLaunch({
          url: '/pages/index/index' // 跳转到首页实现重启效果
        });
      }, 1000)
    },

    // 前往用户信息页面
    toUserInfo() {
      return console.log("ok ~")
      wx.navigateTo({
        url: '/pages/userInfo/userInfo',
      })
    },

  },

  lifetimes: {
    show() {
      console.log('组件所在页面显示');
    },

    created() {
      this.setData({ vip_day: app.globalData.vipDay })
    },

    ready() {},

    attached: function () {
      this.isLogin();
      this.getTotalCountBook();
    }
  }
})